package com.masterclient.app.modules.firstpager.fragments.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.firstpager.GoodProduct;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/6/14 01:28
 * 功能说明：首页好物列表中的图片滑动适配器
 */

public class GoodProductImagePagerAdapter extends PagerAdapter {
    private Context context;
    private List<GoodProduct.DataBean.ShItemsBean.ShImageBean> list;
    GoodProduct.DataBean.ShItemsBean bean;

    GoodProductImagePagerAdapter(Context context, List<GoodProduct.DataBean.ShItemsBean.ShImageBean> list, GoodProduct.DataBean.ShItemsBean bean) {
        this.context = context;
        this.list = list;
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setAdjustViewBounds(true);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewPager.LayoutParams.MATCH_PARENT, ScreenUtils.dp2px(210));
        imageView.setLayoutParams(params);
        GlideUtil.displayImage(context,list.get(position).getSh_image(),imageView,R.mipmap.defaults_1,ScreenUtils.getScreenWidth(),ScreenUtils.dp2px(210));
//        App.picasso.load(list.get(position).getSh_image()).resize(ScreenUtils.getScreenWidth(),ScreenUtils.dp2px(210)).error(R.mipmap.defaults_1).placeholder(R.mipmap.defaults_1).into(imageView);
        container.addView(imageView);
        imageView.setOnClickListener(v->{
            ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                    .withString("id", bean.getSh_product_id())
                    .navigation();
        });
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(((View) object));
    }
}
